package com.sumainfor.common.alipayutils;





import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTExternalSheetNameImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author 支付
 * @ClassName AlipayUtil
 * @date 2019/12/6 18:21
 * @description: TODO
 */
public class AlipayUtil {

    private static final String HTTPURL ="https://www.zfzlpay.com/payment-gate-web/gateway/api/backTransReq";
    public static final String VERSION ="V1.0";
    public static final String PRODUCTID ="0105";//微信公众号支付
    public static final String ZFBPRODUCTID ="0109";//支付宝扫码支付
    public static final String TRANSID ="10";
    public static final String SUBMERNOZF ="8862900100000011005002";
    public static final String SUBMERNOWX ="8862900100000011005001";


    /**
     * 对公(微信)
     * @param params
     * @return
     */
    public static String AliPayPublic(Map<String,Object> params){
        DefaultHttpClient httpClient = null;
        try {
            httpClient = new SSLClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpPost postMethod = new HttpPost(HTTPURL);
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));//请求流水号
        nvps.add(new BasicNameValuePair("version", VERSION));//版本号 3.0为异步通知返回卡类型
        nvps.add(new BasicNameValuePair("productId", PRODUCTID));//产品类型
        //        nvps.add(new BasicNameValuePair("acctNo", "6217001180033516058"));//卡号 0129必传
        //        nvps.add(new BasicNameValuePair("customerName", "李浩"));//持卡人 0129必传
        nvps.add(new BasicNameValuePair("openid", params.get("openid").toString()));//0105必须填写openid。0108时为sub_appid(传进来的openid)
        //       nvps.add(new BasicNameValuePair("userId", "2088802646833765"));//0115  0145必须填写，支付宝的userid  伟华的支付宝  返回的tradeNo截取掉前两位后发送前端进行支付
        nvps.add(new BasicNameValuePair("transId", TRANSID));//交易类型
        nvps.add(new BasicNameValuePair("merNo", params.get("merNo").toString()));//机构商户号
        nvps.add(new BasicNameValuePair("terMerNo", params.get("terMerNo").toString()));//终端商户号
        nvps.add(new BasicNameValuePair("orderDate", params.get("orderDate").toString()));//订单日期
        nvps.add(new BasicNameValuePair("subMerNo", SUBMERNOWX));//商户识别id  8862900100000011005002支付宝   8862900100000011005001微信
        nvps.add(new BasicNameValuePair("orderNo", params.get("orderNo").toString()));//商户订单号
        nvps.add(new BasicNameValuePair("notifyUrl", params.get("notifyUrl").toString()));//异步通知地址
        nvps.add(new BasicNameValuePair("transAmt", params.get("transAmt").toString()));//交易金额，单位 分
        nvps.add(new BasicNameValuePair("commodityName", params.get("commodityName").toString()));//商品名称
        nvps.add(new BasicNameValuePair("commodityDesc", params.get("commodityDesc").toString()));//商品描述
        //        nvps.add(new BasicNameValuePair("storeId", "d10001"));//e0109/0110必须填写，门店编号,需要可区分开门店
        //        nvps.add(new BasicNameValuePair("terminalId", "d10001"));//0109/0110必须填写，商户终端编号,需要可区分开门店
        //        nvps.add(new BasicNameValuePair("limitPay", "no_credit"));//微信支付宝可使用此参数指定不能使用信用卡支付
        nvps.add(new BasicNameValuePair("subChnlMerNo", params.get("subChnlMerNo").toString()));//子商户号100000001399支付宝   100000001397微信
        nvps.add(new BasicNameValuePair("subMerName", params.get("subMerName").toString()));//子商户号名称 必须为实际店名，不得用个人名称代替，否则会存在风控限制
        try {
            nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));//签名
        } catch (Exception e) {
            e.printStackTrace();
        }

        String Attestation = "";
        try {
            postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            HttpResponse resp = httpClient.execute(postMethod);
            String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
            int statusCode = resp.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                boolean signFlag = SignUtils.verferSignData(str);
                if (!signFlag) {
                    System.out.println("验签失败");
                    Attestation = "验签失败";

                }else {
                    System.out.println("验签成功");
                    Attestation = str;
                }
            }else {

                Attestation = "返回失败";
            }

            System.out.println("返回错误码:" + statusCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Attestation;

    }

    /**
     * 扫二维码支付
     * @param params
     * @return
     */
    public static String ZhiFuBaopublic(Map<String,Object> params){

        DefaultHttpClient httpClient = null;
        try {
            httpClient = new SSLClient();
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpPost postMethod = new HttpPost(HTTPURL);
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));//请求流水号
        nvps.add(new BasicNameValuePair("version", VERSION));//版本号 3.0为异步通知返回卡类型
        nvps.add(new BasicNameValuePair("productId", ZFBPRODUCTID));//产品类型
        //nvps.add(new BasicNameValuePair("acctNo", "6217001180033516058"));//卡号 0129必传
        //nvps.add(new BasicNameValuePair("customerName", "李浩"));//持卡人 0129必传
        //nvps.add(new BasicNameValuePair("openid", params.get("openid").toString()));//0105必须填写openid。0108时为sub_appid(传进来的openid)
        //nvps.add(new BasicNameValuePair("userId", "2088802646833765"));//0115  0145必须填写，支付宝的userid  伟华的支付宝  返回的tradeNo截取掉前两位后发送前端进行支付
        nvps.add(new BasicNameValuePair("transId", "10"));//交易类型
        nvps.add(new BasicNameValuePair("merNo", params.get("merNo").toString()));//机构商户号
        nvps.add(new BasicNameValuePair("terMerNo", params.get("terMerNo").toString()));//终端商户号
        nvps.add(new BasicNameValuePair("orderDate", params.get("orderDate").toString()));//订单日期
        nvps.add(new BasicNameValuePair("subMerNo", SUBMERNOZF));//商户识别id  8862900100000011005002支付宝   8862900100000011005001微信
        nvps.add(new BasicNameValuePair("orderNo", params.get("orderNo").toString()));//商户订单号
        nvps.add(new BasicNameValuePair("notifyUrl", params.get("notifyUrl").toString()));//异步通知地址
        nvps.add(new BasicNameValuePair("transAmt", params.get("transAmt").toString()));//交易金额，单位 分
        nvps.add(new BasicNameValuePair("commodityName", params.get("commodityName").toString()));//商品名称
        nvps.add(new BasicNameValuePair("commodityDesc", params.get("commodityDesc").toString()));//商品描述
        nvps.add(new BasicNameValuePair("storeId", params.get("storeId").toString()));//e0109/0110必须填写，门店编号,需要可区分开门店
        nvps.add(new BasicNameValuePair("terminalId", params.get("terminalId").toString()));//0109/0110必须填写，商户终端编号,需要可区分开门店
//        nvps.add(new BasicNameValuePair("storeId", "tangchao0001"));//e0109/0110必须填写，门店编号,需要可区分开门店
//        nvps.add(new BasicNameValuePair("terminalId", "tangchao0001"));//0109/0110必须填写，商户终端编号,需要可区分开门店
        //        nvps.add(new BasicNameValuePair("limitPay", "no_credit"));//微信支付宝可使用此参数指定不能使用信用卡支付
        nvps.add(new BasicNameValuePair("subChnlMerNo", params.get("subChnlMerNo").toString()));//子商户号100000001399支付宝   100000001397微信
        nvps.add(new BasicNameValuePair("subMerName", params.get("subMerName").toString()));//子商户号名称 必须为实际店名，不得用个人名称代替，否则会存在风控限制
        try {
            nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));//签名
        } catch (Exception e) {
            e.printStackTrace();
        }
        String Attestation = "";
        try {
            postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            HttpResponse resp = httpClient.execute(postMethod);
            String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
            int statusCode = resp.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                boolean signFlag = SignUtils.verferSignData(str);
                if (!signFlag) {
                    System.out.println("验签失败");
                    Attestation = "验签失败";

                }else {
                    System.out.println("验签成功");
                    Attestation = str;
                }
            }else {

                Attestation = "返回失败";
            }

            System.out.println("返回错误码:" + statusCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Attestation;

    }


    /**
     * 对私
     * @param params
     * @return
     */
    public static String AliPayPrivate(Map<String,Object> params){

         // 使用返回的htmlInfo
        DefaultHttpClient httpClient = null;
        try {
            httpClient = new SSLClient();
        } catch (Exception e) {
            e.printStackTrace();
        }

        HttpPost postMethod = new HttpPost(HTTPURL);
        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));//请求流水号
        nvps.add(new BasicNameValuePair("version", "新接口V1.0"));//版本号 3.0为异步通知返回卡类型
        nvps.add(new BasicNameValuePair("productId", "0148"));//产品类型
        nvps.add(new BasicNameValuePair("transId", "60"));//
        nvps.add(new BasicNameValuePair("merNo", "888290058111340"));//产品类型
        nvps.add(new BasicNameValuePair("orderDate", params.get("orderDate").toString()));//订单日期
        nvps.add(new BasicNameValuePair("orderNo", params.get("orderNo").toString()));//商户订单号
        nvps.add(new BasicNameValuePair("notifyUrl",params.get("notifyUrl").toString()));//异步通知地址
       nvps.add(new BasicNameValuePair("transAmt",params.get("transAmt").toString() ));//交易金额 分
       nvps.add(new BasicNameValuePair("commodityName",params.get("commodityName").toString() ));//商品名称
        nvps.add(new BasicNameValuePair("phoneNo", "18621599881"));//预留手机号
        nvps.add(new BasicNameValuePair("customerName", "朱裕佳"));//持卡人姓名
        nvps.add(new BasicNameValuePair("cerdType", "01"));//证件类型
        nvps.add(new BasicNameValuePair("cerdId", "310115198610208415"));//证件号
        nvps.add(new BasicNameValuePair("acctNo", "6217001210031557281"));//卡号
        nvps.add(new BasicNameValuePair("frontUrl", params.get("frontUrl").toString()));//前台通知地址
        try {
            nvps.add(new BasicNameValuePair("signature", SignUtils.signDatas(nvps)));//签名
        } catch (Exception e) {
            e.printStackTrace();
        }
        String Attestation = "";
        try {
            postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            HttpResponse resp = httpClient.execute(postMethod);
            String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
            int statusCode = resp.getStatusLine().getStatusCode();
            if (200 == statusCode) {
                boolean signFlag = SignUtils.verferSignDatas(str);
                if (!signFlag) {
                    System.out.println("验签失败");
                    Attestation = "验签失败";

                }else {
                    System.out.println("验签成功");
                    Attestation = str;
                }
            }else {

                Attestation = "返回失败";
            }

            System.out.println("返回错误码:" + statusCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Attestation;
    }


    }

